<?php

class CommentController extends Zend_Controller_Action
{
	function init(){
		Zend_Loader::loadClass('WallModel');
		Zend_Loader::loadClass('CommentModel');
		Zend_Loader::loadClass('UserModel');
		
	}
	function preDispatch() {
    	$auth = Zend_Auth::getInstance();
    	if (!$auth->getIdentity()->id) $this->_redirect('/');
    	
    }
    public function indexAction()
    {
    	$wall_id = $this->_request->getParam('id');
    	$this->view->wall_id = $wall_id;
    	$this->view->wall = WallModel::selectWall( $wall_id );
    	$comment = CommentModel::selectComment( $wall_id );
    	$this->view->comment = $comment;
    	$userList = array();
    	
    	$auth = Zend_Auth::getInstance();
    	$this->view->username = $auth->getIdentity()->username;
    	$this->view->id = $auth->getIdentity()->id;
    	$this->view->avatar = $auth->getIdentity()->avatar;
    	$this->view->fullname = $auth->getIdentity()->fullname;
    	
    	foreach ( $comment as $val ){
    		$tmp = UserModel::getuserinfoID( $val['user_id'] );
    		$userList[] = $tmp->username;
    	}
    	$this->view->userList = $userList;
    }
	public function saveAction()
    {
    	$wall_id = $this->_request->getParam('wall_id');
    	$user_id = $this->_request->getParam('user_id');
    	$content = $this->_request->getParam('content');
    	CommentModel::save( $wall_id, $user_id, $content );
    	exit();
    }
}